package ihm;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;

import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;

import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import kernel.Chariot;
import kernel.Constante;
import kernel.Rail;

import kernel.TapisRoulant;
import kernel.Toboggan;

import vue.VueChariot;
import vue.VueElement;

public class SGBag_BagageDetail extends JDialog {
    @SuppressWarnings("compatibility:5957554103440222243")
    private static final long serialVersionUID = 1L;
    private JTextArea textDetail = new JTextArea();
    private JPanel panalButton = new JPanel();
    private JButton buttonOK = new JButton();
    private JButton buttonCopy = new JButton();
    private VueElement vueElement = null;
    private JPanel panelChariotControl = new JPanel();
    private JPanel panelBagageCreate = new JPanel();
    private JSlider sliderChariotSpeed = new JSlider(0,Constante.VITESSE_MAX,Constante.VITESSE_DEPART);
    private JButton buttonChoiceArc = new JButton();
    private boolean selectRailMode = false;
    private SGBag sGBag = null;
    private JButton buttonCancelSelect = new JButton();
    private JLabel jLabel1 = new JLabel();
    private JTextField jTextField1 = new JTextField();
    private JPanel panelVitsseText = new JPanel();

    public SGBag_BagageDetail() {
        this(null, "", false);
    }

    public SGBag_BagageDetail(Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setSize(new Dimension(238, 449));
        this.getContentPane().setLayout( new BorderLayout() );
        this.setBounds(new Rectangle(10, 10, 235, 400));
        buttonOK.setText("Ok");
        buttonOK.setActionCommand("Ok");
        buttonOK.setFont(new Font("Cambria", 0, 12));
        buttonOK.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    buttonOK_mouseClicked(e);
                }
            });
        buttonCopy.setText("Copier");
        buttonCopy.setFont(new Font("Cambria", 0, 12));
        buttonCopy.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    buttonCopy_mouseClicked(e);
                }
            });
        panelChariotControl.setMinimumSize(new Dimension(133, 50));
        panelChariotControl.setPreferredSize(new Dimension(297, 130));
        panelBagageCreate.setMinimumSize(new Dimension(131, 35));
        panelBagageCreate.setPreferredSize(new Dimension(131, 35));
        buttonChoiceArc.setText("Next Rail");
        buttonChoiceArc.setFont(new Font("Cambria", 0, 12));
        buttonChoiceArc.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    buttonChoiceArc_mouseClicked(e);
                }
            });
        buttonCancelSelect.setText("Cancel Select");
        buttonCancelSelect.setFont(new Font("Cambria", 0, 12));
        buttonCancelSelect.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    buttonCancelSelect_mouseClicked(e);
                }
            });
        jLabel1.setText("Charge ");
        jTextField1.setPreferredSize(new Dimension(70, 21));
        jTextField1.setText("0,035");
        panalButton.setLayout(new BorderLayout());
        panelChariotControl.add(panelVitsseText, null);
        panelChariotControl.add(sliderChariotSpeed, null);
        panelChariotControl.add(buttonChoiceArc, null);
        panelChariotControl.add(buttonCancelSelect, null);
        panelChariotControl.setEnabled(false);
        panalButton.add(panelChariotControl, BorderLayout.CENTER);
        panalButton.add(panelBagageCreate, BorderLayout.SOUTH);
        //panelChariotControl
        panelBagageCreate.add(buttonOK, null);
        panelBagageCreate.add(buttonCopy, null);
        this.getContentPane().add(textDetail, BorderLayout.CENTER);
        this.getContentPane().add(panalButton, BorderLayout.SOUTH);
        panelVitsseText.add(jLabel1, null);
        panelVitsseText.add(jTextField1, null);
        textDetail.setEditable(false);
        textDetail.setLineWrap(true);
        textDetail.setFont(new Font("Consolas", 0, 12));
        textDetail.setSize(new Dimension(233, 250));
        textDetail.setPreferredSize(new Dimension(233, 200));
        sliderChariotSpeed.setMinorTickSpacing(1);
        sliderChariotSpeed.setMajorTickSpacing(2);
        sliderChariotSpeed.setPaintTicks(true);
        sliderChariotSpeed.setPaintLabels(true);
        sliderChariotSpeed.setSnapToTicks(true);
        sliderChariotSpeed.setMaximum(20);
        sliderChariotSpeed.setMinimum(5);
        sliderChariotSpeed.setValue(10);
        sliderChariotSpeed.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    sliderChariotSpeed_stateChanged(e);
                }
            });
    }

    private void buttonOK_mouseClicked(MouseEvent e) {
        /* 
         * fermer la fenetre detail et misa a jour le coefficiant 
         * de la generation de bagage la simulation
         */
        
        // mise en place a l'etat initiale
        buttonCancelSelect_mouseClicked(null);
        this.setVisible(false);
        vueElement = null;
        printDetail();
        
        // mise a jour le coefficiant
        Integer integer = Integer.parseInt(jTextField1.getText().toString());
        if(integer >= 0 ){
            Constante.frequence_aleatoire = Integer.parseInt(jTextField1.getText().toString());
        }
        else { Constante.frequence_aleatoire = Constante.FREQUENCE_ALEATOIRE;}
    }

    private void buttonCopy_mouseClicked(MouseEvent e) {
        // copier le text dans le Presse-papiers
        StringSelection stsel = new StringSelection(this.textDetail.getText());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);
    }

    public JTextArea getTextDetail() {
        return textDetail;
    }
    
    public void printDetail(){
        // afficher l'infomation correspond a l'object choisi
        if (vueElement != null){
            textDetail.setText(vueElement.getInformation());
            if (vueElement instanceof VueChariot) {
                panelChariotControl.setEnabled(true);
            }
            else {
                panelChariotControl.setEnabled(false);
            }
        }
    }

    public void setVueElement(VueElement vueElement) {
        this.vueElement = vueElement;
    }

    public VueElement getVueElement() {
        return vueElement;
    }

    private void buttonChoiceArc_mouseClicked(MouseEvent e) {
        selectRailMode = true;
        sGBag.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    public void setSGBag(SGBag sGBag) {
        this.sGBag = sGBag;
    }

    public SGBag getSGBag() {
        return sGBag;
    }

    public boolean isSelectRailMode() {
        return selectRailMode;
    }

    void selectRail(Rail rail) {
        if (rail == null){
            
        }
        else if((selectRailMode)&&(vueElement instanceof VueChariot)){
            //editorPaneNoeudDepart.setText("TapisRoulant : " + noeudDepart.getId());
            if(((VueChariot)vueElement).getChariot().getRail().getNoeudFin().getListArcDep().contains(rail))
            ((VueChariot)vueElement).getChariot().changerRail(rail);
            selectRailMode = false;
        }
        if (!selectRailMode){
            sGBag.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }


    private void sliderChariotSpeed_stateChanged(ChangeEvent e) {
        // lorsqu'on modifie la vitesse manuellement, affecter cette vitesse a la simulation
        if (vueElement instanceof VueChariot){
            VueChariot c = (VueChariot) vueElement;
            int newSpeed = sliderChariotSpeed.getValue();
            Constante.vitesse_max = newSpeed;
            printDetail();
        }
    }

    private void buttonCancelSelect_mouseClicked(MouseEvent e) {
        selectRailMode = false;
        sGBag.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
}
